from random import choice  # 随机数模块
import re  # 正则表达式
import urllib.request  # urllib工具包里的request模块

opener = urllib.request.build_opener(urllib.request.HTTPSHandler())
useragentList = ["Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
                 "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1",
                 "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Mobile Safari/537.36"]
# 若干个用户代理信息（可以理解为设备信息）
urr = urllib.request.Request(r"https://www.youdao.com/", headers={"User-Agent": choice(useragentList)})
html_code = opener.open(urr).read().decode()  # 获取源代码

string = re.findall(r"<title>(.*?)</title>", html_code)  # 从源代码中获取标题
print(string)  # 打印标题
